package guiPersonale;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

import main.DBConnection;
import net.miginfocom.swing.MigLayout;

import javax.swing.JComboBox;

import domain.Muligheder;
import eksterneKlasser.ListTableModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class RedigerVare extends JPanel implements ActionListener {
	
	private JComboBox<?> comboBox; 
	CardLayout cl = new CardLayout();
	private JTable table;
	DBConnection con;
	ListTableModel model;
	String sql;
	String sqlUpdate;
	String sqlDelete;
	private JTextField textNavn = new JTextField(10);
	private JTextField textPris = new JTextField(10);
	private JButton ok = new JButton("OK");
	private JButton delete = new JButton("Slet");
	private double prisParse;
	private int currentId;
	
	public RedigerVare() throws FileNotFoundException, IOException, SQLException{
		setLayout(new MigLayout("", "[grow][grow]", "[][][grow]"));
		setBorder(BorderFactory.createLineBorder(Color.BLACK));
		JLabel title = new JLabel("Rediger Vare");
		title.setFont(new Font("Tahoma", Font.PLAIN, 22));
		add(title, "cell 0 0");
	
		JLabel lblVlgKategori = new JLabel("V\u00E6lg kategori:");
		add(lblVlgKategori, "flowx,cell 0 1,alignx left");
		Muligheder m = new Muligheder();
		comboBox = new JComboBox(m.getKategorier());
		comboBox.addActionListener(this);
		
		
		
		con = new DBConnection();
		con.createConnection();

		sql = "Select id, name, price from forretter";
		try {
			Statement stmt = con.connection.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			model = ListTableModel.createModelFromResultSet(rs);
			}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		table = new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2){
					JTable target = (JTable) e.getSource();
					int row = target.getSelectedRow();
					int column = target.getSelectedColumn();
					String navn = (String) target.getValueAt(row, 1);
					double pris = (double) target.getValueAt(row, 2);
					currentId = (int) target.getValueAt(row, 0);
					textNavn.setText(navn);
					textPris.setText(String.valueOf(pris));
					redigerVareFrame rvf = new redigerVareFrame();
				}
			}
		});
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scrollPane = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		add(comboBox, "cell 0 1");
		add(scrollPane,"cell 0 2 2 1");
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		int temp;
		temp = comboBox.getSelectedIndex();
		switch (temp) {
		case 0:
			con = new DBConnection();
			try {
				con.createConnection();
			} catch (IOException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			sql = "Select id, name, price from forretter";
			try {
				Statement stmt = con.connection.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				model = ListTableModel.createModelFromResultSet(rs);
				}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			table.setModel(model);
			break;
		case 1:
			con = new DBConnection();
			try {
				con.createConnection();
			} catch (IOException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			sql = "Select id, name, price from hovedretter";
			try {
				Statement stmt = con.connection.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				model = ListTableModel.createModelFromResultSet(rs);
				}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			table.setModel(model);
			break;
		case 2:
			con = new DBConnection();
			try {
				con.createConnection();
			} catch (IOException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			sql = "Select id, name, price from vegetarretter";
			try {
				Statement stmt = con.connection.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				model = ListTableModel.createModelFromResultSet(rs);
				}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			table.setModel(model);
			break;
		case 3:
			con = new DBConnection();
			try {
				con.createConnection();
			} catch (IOException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			sql = "Select id, name, price from biryani";
			try {
				Statement stmt = con.connection.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				model = ListTableModel.createModelFromResultSet(rs);
				}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			table.setModel(model);
			break;
		case 4:
			con = new DBConnection();
			try {
				con.createConnection();
			} catch (IOException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			sql = "Select id, name, price from sandwichbagels";
			try {
				Statement stmt = con.connection.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				model = ListTableModel.createModelFromResultSet(rs);
				}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			table.setModel(model);
			break;
		case 5:
			con = new DBConnection();
			try {
				con.createConnection();
			} catch (IOException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			sql = "Select id, name, price from tilbehor";
			try {
				Statement stmt = con.connection.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				model = ListTableModel.createModelFromResultSet(rs);
				}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			table.setModel(model);
			break;
		case 6:
			con = new DBConnection();
			try {
				con.createConnection();
			} catch (IOException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			sql = "Select id, name, price from drikkevare";
			try {
				Statement stmt = con.connection.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				model = ListTableModel.createModelFromResultSet(rs);
				}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			table.setModel(model);
			break;
		}
	}
	public class redigerVareFrame extends JFrame{
		public redigerVareFrame(){
			setTitle("Rediger Vare");
			setBounds(100, 100, 300, 200);
			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(new MigLayout("","[grow][grow]","[][][][][][grow]"));
			setVisible(true);
			ok.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					int temp;
					DBConnection connec = new DBConnection();
					temp = comboBox.getSelectedIndex();
					switch (temp) {
					case 0:
						try {
							
							connec.createConnection();
						} catch (IOException | SQLException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
						prisParse = Double.parseDouble(textPris.getText());
						sqlUpdate = "update forretter set Name = ?, Price = ? where ID = ?";
						try {
							PreparedStatement stmt = con.connection.prepareStatement(sqlUpdate);
							
							stmt.setString(1, textNavn.getText());
							stmt.setDouble(2, prisParse);
							stmt.setInt(3, currentId);
							stmt.executeUpdate();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							
						}
						setVisible(false);
						break;
					case 1:
						try {
							connec.createConnection();
						} catch (IOException | SQLException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
						prisParse = Double.parseDouble(textPris.getText());
						sqlUpdate = "update hovedretter set Name = ?, Price = ? where ID = ?";
						try {
							PreparedStatement stmt = con.connection.prepareStatement(sqlUpdate);
							
							stmt.setString(1, textNavn.getText());
							stmt.setDouble(2, prisParse);
							stmt.setInt(3, currentId);
							stmt.executeUpdate();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							
						}
						setVisible(false);
						break;
					case 2:
						try {
							connec.createConnection();
						} catch (IOException | SQLException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
						prisParse = Double.parseDouble(textPris.getText());
						sqlUpdate = "update vegetarretter set Name = ?, Price = ? where ID = ?";
						try {
							PreparedStatement stmt = con.connection.prepareStatement(sqlUpdate);
							
							stmt.setString(1, textNavn.getText());
							stmt.setDouble(2, prisParse);
							stmt.setInt(3, currentId);
							stmt.executeUpdate();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							
						}
						setVisible(false);
						break;
					case 3:
						try {
							connec.createConnection();
						} catch (IOException | SQLException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
						prisParse = Double.parseDouble(textPris.getText());
						sqlUpdate = "update biryani set Name = ?, Price = ? where ID = ?";
						try {
							PreparedStatement stmt = con.connection.prepareStatement(sqlUpdate);
							
							stmt.setString(1, textNavn.getText());
							stmt.setDouble(2, prisParse);
							stmt.setInt(3, currentId);
							stmt.executeUpdate();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							
						}
						setVisible(false);
						break;
					case 4:
						try {
							connec.createConnection();
						} catch (IOException | SQLException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
						prisParse = Double.parseDouble(textPris.getText());
						sqlUpdate = "update sanwichbagels set Name = ?, Price = ? where ID = ?";
						try {
							PreparedStatement stmt = con.connection.prepareStatement(sqlUpdate);
							
							stmt.setString(1, textNavn.getText());
							stmt.setDouble(2, prisParse);
							stmt.setInt(3, currentId);
							stmt.executeUpdate();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							
						}
						setVisible(false);
						break;
					case 5:
						try {
							connec.createConnection();
						} catch (IOException | SQLException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
						prisParse = Double.parseDouble(textPris.getText());
						sqlUpdate = "update tilbehor set Name = ?, Price = ? where ID = ?";
						try {
							PreparedStatement stmt = con.connection.prepareStatement(sqlUpdate);
							
							stmt.setString(1, textNavn.getText());
							stmt.setDouble(2, prisParse);
							stmt.setInt(3, currentId);
							stmt.executeUpdate();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							
						}
						setVisible(false);
						break;
					case 6:
						try {
							connec.createConnection();
						} catch (IOException | SQLException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
						prisParse = Double.parseDouble(textPris.getText());
						sqlUpdate = "update drikkevare set Name = ?, Price = ? where ID = ?";
						try {
							PreparedStatement stmt = con.connection.prepareStatement(sqlUpdate);
							
							stmt.setString(1, textNavn.getText());
							stmt.setDouble(2, prisParse);
							stmt.setInt(3, currentId);
							stmt.executeUpdate();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							
						}
						setVisible(false);
						break;
					}
				}
			});
			delete.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					int sletVareTjek = JOptionPane.showConfirmDialog(delete, "Vil du slette denne vare?");
					if(sletVareTjek == JOptionPane.YES_OPTION){
						int temp;
						DBConnection connec = new DBConnection();
						
						temp = comboBox.getSelectedIndex();
						switch (temp) {
						case 0:
							sqlDelete = "delete from forretter where Name = ?";
							try {
								connec.createConnection();
								PreparedStatement stmt = connec.connection.prepareStatement(sqlDelete);
								stmt.setString(1, textNavn.getText());
								stmt.executeUpdate();
								
							} catch (SQLException | IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							setVisible(false);
							break;
						case 1:
							sqlDelete = "delete from hovedretter where Name = ?";
							try {
								connec.createConnection();
								PreparedStatement stmt = connec.connection.prepareStatement(sqlDelete);
								stmt.setString(1, textNavn.getText());
								stmt.executeUpdate();
								
							} catch (SQLException | IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							setVisible(false);
							break;
						case 2:
							sqlDelete = "delete from vegetarretter where Name = ?";
							try {
								connec.createConnection();
								PreparedStatement stmt = connec.connection.prepareStatement(sqlDelete);
								stmt.setString(1, textNavn.getText());
								stmt.executeUpdate();
								
							} catch (SQLException | IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							setVisible(false);
							break;
						case 3:
							sqlDelete = "delete from biryani where Name = ?";
							try {
								connec.createConnection();
								PreparedStatement stmt = connec.connection.prepareStatement(sqlDelete);
								stmt.setString(1, textNavn.getText());
								stmt.executeUpdate();
								
							} catch (SQLException | IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							setVisible(false);
							break;
						case 4:
							sqlDelete = "delete from sandwichbagels where Name = ?";
							try {
								connec.createConnection();
								PreparedStatement stmt = connec.connection.prepareStatement(sqlDelete);
								stmt.setString(1, textNavn.getText());
								stmt.executeUpdate();
								
							} catch (SQLException | IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							setVisible(false);
							break;
						case 5:
							sqlDelete = "delete from tilbehor where Name = ?";
							try {
								connec.createConnection();
								PreparedStatement stmt = connec.connection.prepareStatement(sqlDelete);
								stmt.setString(1, textNavn.getText());
								stmt.executeUpdate();
								
							} catch (SQLException | IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							setVisible(false);
							break;
						case 6:
							sqlDelete = "delete from drikkevare where Name = ?";
							try {
								connec.createConnection();
								PreparedStatement stmt = connec.connection.prepareStatement(sqlDelete);
								stmt.setString(1, textNavn.getText());
								stmt.executeUpdate();
								
							} catch (SQLException | IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							setVisible(false);
							break;
						}
					}
					
					
						
				}
			});
			add(new JLabel("Navn"),"cell 0 3");
			add(textNavn,"wrap, cell 1 3");
			add(new JLabel("Pris"),"cell 0 4");
			add(textPris, "cell 1 4");
			add(ok,"cell 2 6");
			add(delete,"cell 2 6");
			
			
		}
	}

}
